package com.goodlearning.gloriamobile.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.goodlearning.gloriamobile.R;
import com.goodlearning.gloriamobile.commons.Constants;
import com.goodlearning.gloriamobile.commons.Utils;
import com.goodlearning.gloriamobile.dto.GetNoticeResultDto;
/**
 * 상세  WebPage 호출 및 출력
 * noticeview.asp 라는데 이게 멀까 씨벌
 * @author rapty54
 *
 */
public class NotificationDetailWebview extends BaseActivity{
	
	WebView noticeWeView;
	
	private ProgressBar progressBar;
	
	int count=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_detail_webview);
		CookieSyncManager.createInstance(this);
		
		init();
	}
	
	public void init(){
		baseTitleLeftBtn.setVisibility(View.INVISIBLE);
		baseTitleTv.setText("공지사항 상세보기");
		baseTitleRightBtn.setVisibility(View.INVISIBLE);
		
		final GetNoticeResultDto noticeDto = getIntent().getParcelableExtra("getNoticeResultDto");
		
		Utils.log("Request_Notice:"+"["+noticeDto.getNoticePage()+"]"+"Request_idx:"+"["+noticeDto.getIdx()+"]");
		
		showDetailWebView(noticeDto.getIdx(), noticeDto.getNoticePage()
		,R.id.notofication_detail_content_WebView,R.id.progress_web_01
		,NotificationDetailWebview.this);
		
		if(!noticeDto.getFilename().equals("")){
			
			Utils.log("File Name:"+"["+noticeDto.getFilename()+"]");
			
			Utils.showConfirmDialog(NotificationDetailWebview.this, "  확인", "첨부파일이 존재하는 공지입니다.\n첨부 파일 확인은 웹사이트를 이용해 주십시요.",new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					
				}
			} , null);
		}
	}
	
	/**
	 *  WebView 요청시 IDX 와  NoticePage 를 동시에 요청하여 넣어야한다. 
	 * @param idx
	 * @param noticePage
	 */
	
	public void showDetailWebView(String idx , String noticePage , int resourceId,int progressId,final Context context){
		
		noticeWeView=(WebView)findViewById(resourceId);
		progressBar = (ProgressBar) findViewById(progressId);
		noticeWeView.clearCache(true);
//		noticeWeView.setWebViewClient(new WebClientManager(NotificationDetailWebview.this, progressBar, noticeWeView));
		noticeWeView.setInitialScale(1);
		
		WebSettings webSettings = noticeWeView.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
		webSettings.setSaveFormData(true);
		webSettings.setSavePassword(true);
		webSettings.setUseWideViewPort(true);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
		webSettings.setBuiltInZoomControls(true);
		
		Utils.log(Constants.Request_URL+"idx="+idx+"&"+"noticetype="+noticePage);
		
		noticeWeView.loadUrl(Constants.Request_URL+"idx="+idx+"&"+"noticetype="+noticePage);
		noticeWeView.setVisibility(View.VISIBLE);
		noticeWeView.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if(count==1){
					
					Toast.makeText(context, "화면을 두번 터치하면 화면비로 보기가 가능합니다.", Toast.LENGTH_SHORT).show();
				
				}
				
				count++;
				return false;
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		CookieSyncManager.getInstance().startSync();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		CookieSyncManager.getInstance().stopSync();
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.removeAllCookie();
		noticeWeView.destroy();
	}
	
}
